package Client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import Server.Server;

public class Login extends JFrame {
	/** * 登陆窗口 */
	private static final long serialVersionUID = 1L;
	private JLabel jLabel1, jLabel2, jLabel3;
	private JTextField userName, serverAddress, port;
	private JButton jLogin, jReset;
	private JPanel panel;

	// 初始化各组件
	private void init() {
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel1.setText("用户名");
		jLabel2.setText("服务器");
		jLabel3.setText("端口号");
		userName = new JTextField();
		serverAddress = new JTextField();
		port = new JTextField();
		port.addKeyListener(new KeyListener() {

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				int keycode = e.getKeyChar();
				if (keycode < 48 || keycode > 57) {
					e.consume();
				}
				if(((JTextField)e.getSource()).getText().length()>3){
					e.consume();
				}
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		userName.setColumns(20);
		serverAddress.setColumns(20);
		port.setColumns(20);
		userName.setText("");
		serverAddress.setText("");
		port.setText("");
		jLogin = new JButton();
		jReset = new JButton();
		jLogin.setText("登陆");
		jReset.setText("重置");
		jLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Login.this.excute();
			}
		});
		jReset.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Login.this.reset();
			}
		});
		panel = new JPanel();
		panel.add(jLabel1);
		panel.add(userName);
		panel.add(jLabel2);
		panel.add(serverAddress);
		panel.add(jLabel3);
		panel.add(port);
		panel.add(jLogin);
		panel.add(jReset);
		panel.setBorder(BorderFactory.createTitledBorder(null, "用户登录",
				TitledBorder.CENTER, TitledBorder.ABOVE_TOP, new Font(
						"宋体", Font.BOLD, 20), Color.red));
		this.add(panel);
		setSize(300,200);
		setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
	}

	private void reset() {
		userName.setText(null);
		serverAddress.setText(null);
		port.setText("5000");
	}

	private void excute() {
		if (Login.this.userName.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "请先输入用户名", "无法登录",
					JOptionPane.WARNING_MESSAGE);
			return;
		};
		if (Login.this.serverAddress.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "请先输入服务器地址", "无法登录",
					JOptionPane.WARNING_MESSAGE);
			return;
		};
		if (Login.this.port.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "请先输入端口号", "无法登录",
					JOptionPane.WARNING_MESSAGE);
			return;
		};
		new LoginConnection(userName.getText(), serverAddress.getText(),
				Integer.parseInt(port.getText()), this);
	}

	public Login(String name) {
		super(name);
		this.init();
	}

	public static void main(String args[]) {
		new Login("登录");
	}
}
